# -*- coding: utf-8 -*-
#exec_post_task.py

import wsgiref.handlers
from google.appengine.ext import webapp
import sys, time, uuid
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from xmw import tianya
from xmw.tianya.bot import *
import xmw
from xmw.tianya import error_tianya
import urllib2
import logging
class Head_Table():
    def __init__(self,table):
        self.table=table
        #print "!%s!"%repr(table.contents)
        logging.log(logging.INFO, "!%s!"%repr(table.contents))
        self.parse()
        
    def parse(self):
        first_td=self.table.tr.td
        follow_tds=first_td.fetchNextSiblings()
        self.floor_info=follow_tds.pop()
        self.author_info=follow_tds.pop()

        
    def get_post_date(self):
        front=self.author_info.findChild()
        d =front.contents[2]
        logging.log(logging.INFO, "D%sD"%repr(d))
        #print d
        ss=d.split(u"\uff1a")
        #ss=d.split(u'\xba')
        post_date=unicode_to_datetime(ss[1])
        
        return post_date
    def get_author(self):
        front=self.author_info.findChild()
        #print front.contents
        a=front.findChild()
        logging.info("author type:%s"%type(a.contents))
        #return str(a.contents[0]).decode('gb18030')
        #a.contents.append(str(a.contents[0]).decode('gb18030'))
        return str(a.contents[0]).decode('utf-8')

    def get_floor(self):
        front=self.floor_info.findChild()
        #print self.floor_info
        floor=front.contents[0]
        
        logging.info("floor type:%s"%type(floor))
        return str(floor).decode('utf-8')
    
class MainPage(xmw.RequestHandler):
    def get(self):
        e=None
        q=Task_ScanPost.all()
        #q.filter('is_locked=',False)
        q.filter('status =',Task_ScanPost_Status.WAIT)
        
        task=q.get()
        if task is None:
            e=error_tianya.NO_SCANPOST_TASK('/exec_post_task')
            self.render_error(e)
            return
        
        #firstly set the task status
        task.status=Task_ScanPost_Status.WORK
        task.put()
        
        #now to exec the task
        url=task.page_url[0]
        
        page=urllib2.urlopen(url)
        soup=BeautifulSoup(page,fromEncoding="GB18030")
        if task.offset >= task.page_cnt:
            task.status=Task_ScanPost_Status.FINISHED
            task.put()
            e=error_tianya.MISSION_ACCOMPLISHED('pages:%s,urls:%s'%(str(task.page_cnt),task.page_url))
            self.render_error(e)
            return
        
            
        
        
        """
        #should be checked when to generate the task
        type=get_page_type(soup)
        
        if type != PAGE_TYPE.FORM:
            e=error_tianya.NOT_SUPPORTED_THREAD(url)
            task.is_locked=False
            task.put()
            self.render_error(e)
            return
        """
        lst_table=soup.findAll('table',{'align':'center','width':'100%'})
        lst_content=soup.findAll('div',{'class':'content','style':'WORD-WRAP:break-word'})
        
        post_cnt=len(lst_content)
        
        index=0
        while index < post_cnt:
            table=lst_table[index+1]
            ht=Head_Table(table)
            content=lst_content[index]
            logging.info("CONTENT:%s"%repr(content))
            content1=str(content.contents[0]).decode("utf-8")
            #content2=content1.encode('ascii')
            logging.info("url=%s"%url)
            #ht.get_author(),\
            uid=xmw.get_uuid_hex()
            post=Post(author=ht.get_author(),\
                      date=ht.get_post_date(),\
                      floor=ht.get_floor(),\
                      #floor="floor",\
                      content=content1,\
                      #content="content",\
                      url=(repr(url)).encode('ascii'),\
                      post_id=uid)
            post.put()
            index=index+1
            
        
        task.offset=task.offset+1
        task.status=Task_ScanPost_Status.WAIT
        
        #Da Wan Shou Gong
        task.put()
        
        
        e=error_tianya.SCAN_ONE_PAGE('url:%s,posts:%s'%(url,str(index)))
        self.render_error(e)
    
def main():
    import global_setting
    
    application = webapp.WSGIApplication(
                                       [('/exec_post_task', MainPage)],
                                       debug=global_setting.is_debug())                                       
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()
